import {
  list,find
} from '../../../api/wxPage'
import {
  list as designerList
} from '../../../api/designer'
const util = require('../../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 1,
    length:'',
    isSwiperShow:true,
    componentsList: [],
    startSwipers: false,
    secondSwipers: false,
    thirdSwipers: false,
    fourthSwipers: false,
    fifthSwipers: false,
    isShow: true,
    lastTime:null,
    mchId:null,
    backgroundColor:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNavSize()
    app.userInfoReadyCallback = res => {
      if (app.globalData.token != null) {
        util.forwarding(options,app.globalData.token)
      }
    }
  },
  onShow: function () {
    this.loadWxpage();
  },
  loadWxpage(){

    let curTime = new Date().getTime();
    if (this.data.lastTime!=null && app.globalData.mchId==this.data.mchId) {
      if (this.data.lastTime>(curTime-1800)) {
        return;
      }
     }
     this.data.lastTime = curTime;
    list({
      mchId: app.globalData.mchId,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      sortField: 'orders',
      direction: 'asc'
    }).then(res => {
      console.log(res)
      this.data.mchId = app.globalData.mchId
      this.loadDesigner(res.data.data[0].id)
      this.getPageInfo(res.data.data[0].id)
    })
  },

    getPageInfo(id){
        find(id).then(res=>{
            console.info(res)
        if(res.data.wxPage.hasOwnProperty('backgroundColor') && !util.isNull(res.data.wxPage.backgroundColor)){
            this.setData({
                backgroundColor:res.data.wxPage.backgroundColor,
            })
        }
        if(res.data.wxPage.hasOwnProperty('shareImage') && !util.isNull(res.data.wxPage.shareImage)){
            this.data.shareImage = res.data.wxPage.shareImage
            this.setData({
                shareImage:res.data.wxPage.shareImage
            })
        }
        if(res.data.wxPage.hasOwnProperty('shareDescr') && !util.isNull(res.data.wxPage.shareDescr)){
            this.data.shareDescr = res.data.wxPage.shareDescr
            this.setData({
                shareDescr:res.data.wxPage.shareDescr
            })
        }
    })
},

  loadDesigner(data) {
    var _this = this
    designerList({
      mchId: app.globalData.mchId,
      pageId: data,
    }).then(res => {
      console.log(res)
      _this.data.componentsList = res.data
      _this.data.length=res.data.length
      _this.setData({
        componentsList: _this.data.componentsList,
      })
    })
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
    console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  onPageScroll: function (e) {

   if(e.scrollTop>217){
        this.data.isSwiperShow=false

   }else{
     this.data.isSwiperShow=true
   }
   this.setData({
    isSwiperShow:this.data.isSwiperShow
   })
  },
  onPullDownRefresh() {
    var _this=this
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      _this.data.componentsList.forEach(item=>{
        if (item.name==='products') {
          _this.selectComponent('#products').downRefresh();
        }else if (item.name==='productRow') {
          _this.selectComponent('#productRow').downRefresh();
        }else if (item.name==='groupProduct') {
          _this.selectComponent('#groupProduct').downRefresh();
        }else if (item.name==='conciseProductRow') {
          _this.selectComponent('#conciseProductRow').downRefresh();
        }else if (item.name==='conciseProduct') {
          _this.selectComponent('#conciseProduct').downRefresh();
        }else if (item.name==='groupProductRow') {
          _this.selectComponent('#groupProductRow').downRefresh();
        }else if (item.name==='bargainProduct') {
          _this.selectComponent('#bargainProduct').downRefresh();
        }else if (item.name==='bargainProductRow') {
          _this.selectComponent('#bargainProductRow').downRefresh();
        }else if (item.name=='videocontent') {
          _this.selectComponent('#videocontent').downRefresh();
        }
      })
      wx.stopPullDownRefresh()
    }, 1000);
  },
  onReachBottom() {
     this.data.componentsList.forEach(item=>{
       if(item.name=='videocontent'){
        this.selectComponent('#videocontent').onReachBottom();
       }
     })
    if(this.data.componentsList.length>0){
      if (this.data.componentsList[this.data.length - 1].name == 'products') {
        this.selectComponent('#products').onReachBottom();
       }else if(this.data.componentsList[this.data.length - 1].name == 'productRow'){
         this.selectComponent('#productRow').onReachBottom();
       }else if(this.data.componentsList[this.data.length - 1].name == 'groupProduct'){
        this.selectComponent('#groupProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'conciseProductRow'){
        this.selectComponent('#conciseProductRow').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'conciseProduct'){
        this.selectComponent('#conciseProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'groupProductRow'){
        this.selectComponent('#groupProductRow').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'bargainProduct'){
        this.selectComponent('#bargainProduct').onReachBottom();
      }else if(this.data.componentsList[this.data.length - 1].name == 'bargainProductRow'){
        this.selectComponent('#bargainProductRow').onReachBottom();
      }
      
    }
  },
  searchClick() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    })
  }
    ,
    onShareAppMessage: function () {
        let shareImage = util.isNull(this.data.shareImage)?"":util.thumbnail(this.data.shareImage,500,400)
        let shareDescr = util.isNull(this.data.shareDescr)?app.globalData.mchName:this.data.shareDescr
        return {
            title: shareDescr,
            desc: '分享页面的内容',
            imageUrl:shareImage,
            path: '/pages/index/wxPage/wxPage?id=' + this.data.id + '&xuid=' + app.globalData.userInfo.id
        }
    },
    onShareTimeline: function () {
        let shareImage = util.isNull(this.data.shareImage)?"":util.thumbnail(this.data.shareImage,500,400)
        let shareDescr = util.isNull(this.data.shareDescr)?app.globalData.mchName:this.data.shareDescr
        return {
            title: shareDescr,
            desc: '分享页面的内容',
            path: '/pages/index/wxPage/wxPage',
            imageUrl: shareImage,
            query: 'id=' + this.data.id + '&xuid=' + app.globalData.userInfo.id
        }
    }
})